#include "TableInterface/table.h"
#include "function_interface.h"
#include "fpga_interface.h"
#include "comm_monitor.h"
#include "electricity_quantity_statistics.h"
#include "global_variable.h"
#include "Product.h"
#include <time.h>
#include "debug.h"
#include "syslog.h"


static uint16_t lcInStatePrev = 0;


int LcTable_InitCallback(tableEntry_t *pEntry)
{
    int retVal = 0;
    uint32_t addr = pEntry->idx;
    uint16_t *pValue = &pEntry->data;

    switch(addr)
    {        
        default:
            *pValue = 0;
            return -1;
    }

    return retVal;
}


int LcTable_RefreshCallback(tableEntry_t *pEntry)
{
    int retVal = 0;
    uint32_t addr = pEntry->idx;
    uint16_t *pValue = &pEntry->data;
    funcInfDevState_t DevState;

    switch(addr)
    {
        case 20026:
        {
            *pValue = 20026;
            #if 1
            FuncInf_GetDevState(NULL, &DevState);
            if (DevState == FUNCINF_DEV_STATE_SHUTDOWN)
            {
                *pValue = 0;
            }
            else if (DevState == FUNCINF_DEV_STATE_STANDBY)
            {
                *pValue = 1;
            }
            else if (DevState == FUNCINF_DEV_STATE_CHRGING)
            {
                *pValue  = 2;
            }
            else if (DevState == FUNCINF_DEV_STATE_DISCING)
            {
                *pValue  = 3;
            }
            else if (DevState == FUNCINF_DEV_STATE_FAULT)
            {
                *pValue  = 4;
            }
            #endif
            break;
        }

        case 10010:
        {
            FuncInf_GetTime(FUNCINF_TIME_TYPE_YEAR, pValue);
            break;
        }

        case 10011:
        {
            FuncInf_GetTime(FUNCINF_TIME_TYPE_MON, pValue);
            break;
        }

        case 10012:
        {
            FuncInf_GetTime(FUNCINF_TIME_TYPE_DATE, pValue);
            break;
        }

        case 10013:
        {
            FuncInf_GetTime(FUNCINF_TIME_TYPE_HOUR, pValue);
            break;
        }

        case 10014:
        {
            FuncInf_GetTime(FUNCINF_TIME_TYPE_MIN, pValue);
            break;
        }

        case 10015:
        {
            FuncInf_GetTime(FUNCINF_TIME_TYPE_SEC, pValue);
            break;
        }
    }
    return retVal;
}


int LcTable_SetCallback(tableEntry_t *pEntry)
{
    int retVal = 0;
    int32_t tempI32 = 0;
    uint32_t addr = pEntry->idx;
    uint16_t *pValue = &pEntry->data, lcInState;
    funcInfCtrlMode_t ctrlmode = 0;
    funcInfRunMode_t runmode = 0;

    switch(addr)
    {
        case 0:/*  LC下发的控制模式 */
        {
            printf("[%s][%d] addr = %d\n", __func__, __LINE__, addr);
            #if 1
            if(*pValue == 0x1111 && ctrlmode != FUNCINF_CTRL_MODE_REMOTE)
            {
                FuncInf_SetCtrlMode(FUNCINF_OPERATOR_LC, NULL, FUNCINF_CTRL_MODE_REMOTE);
            }
            else if(*pValue == 0x4444 && ctrlmode != FUNCINF_CTRL_MODE_LOCAL) 
            {
                FuncInf_SetCtrlMode(FUNCINF_OPERATOR_LC, NULL, FUNCINF_CTRL_MODE_LOCAL);
            }
            #endif
            break;
        }

        case 1:/* LC下发的运行模式 */
        {
            printf("[%s][%d] addr = %d\n", __func__, __LINE__, addr);
            #if 1
            FuncInf_GetRunMode(NULL, &runmode);
            if(*pValue == FUNCINF_RUN_MODE_PQ && runmode != FUNCINF_RUN_MODE_PQ) 
            {
                FuncInf_SetRunMode(FUNCINF_OPERATOR_LC, NULL, FUNCINF_RUN_MODE_PQ);
            }
            else if(*pValue == FUNCINF_RUN_MODE_VF && runmode != FUNCINF_RUN_MODE_VF) 
            {
                FuncInf_SetRunMode(FUNCINF_OPERATOR_LC, NULL, FUNCINF_RUN_MODE_VF);
            }
            else if(*pValue == FUNCINF_RUN_MODE_VSG && runmode != FUNCINF_RUN_MODE_VSG) 
            {
                FuncInf_SetRunMode(FUNCINF_OPERATOR_LC, NULL, FUNCINF_RUN_MODE_VSG);
            }
            #endif
            break;
        }

        case 2:/* LC下发的开关机 */
        {
            printf("[%s][%d] addr = %d\n", __func__, __LINE__, addr);
            #if 1
            if(*pValue == 0x55AA)
            {
                FuncInf_TurnOnOff(FUNCINF_OPERATOR_LC, NULL, FUNCINF_TURN_OFF);
            }
            else if(*pValue == 0xAA55)
            {
                GlobalVariable_GetInt32(GV_INT32_LC_IN_FAULT, &tempI32);
                if(tempI32 != 0)
                {
                    printf("禁止开机：存在升压变故障!\n");
                    break;
                }
                else
                {
                    FuncInf_TurnOnOff(FUNCINF_OPERATOR_LC, NULL, FUNCINF_TURN_ON);
                }
            }
            #endif
            break;
        }

        case 10:/* LC下发的年 */
        {
            printf("[%s][%d] addr = %d\n", __func__, __LINE__, addr);
            #if 1
            FuncInf_SetTime(FUNCINF_OPERATOR_LC, FUNCINF_TIME_TYPE_YEAR, *pValue);
            #endif
            break;
        }

        case 11: //LC下发的月
        {
            printf("[%s][%d] addr = %d\n", __func__, __LINE__, addr);
            #if 1
            FuncInf_SetTime(FUNCINF_OPERATOR_LC, FUNCINF_TIME_TYPE_MON, *pValue);
            #endif
            break;
        }

        case 12: //LC下发的日
        {
            printf("[%s][%d] addr = %d\n", __func__, __LINE__, addr);
            #if 1
            FuncInf_SetTime(FUNCINF_OPERATOR_LC, FUNCINF_TIME_TYPE_DATE, *pValue);
            #endif
            break;
        }

        case 13: //LC下发的时
        {
            printf("[%s][%d] addr = %d\n", __func__, __LINE__, addr);
            #if 1
            FuncInf_SetTime(FUNCINF_OPERATOR_LC, FUNCINF_TIME_TYPE_HOUR, *pValue);
            #endif
            break;
        }

        case 14: //LC下发的分
        {
            printf("[%s][%d] addr = %d\n", __func__, __LINE__, addr);
            #if 1
            FuncInf_SetTime(FUNCINF_OPERATOR_LC, FUNCINF_TIME_TYPE_MIN, *pValue);
            #endif
            break;
        }

        case 15: //LC下发的秒
        {
            printf("[%s][%d] addr = %d\n", __func__, __LINE__, addr);
            #if 1
            FuncInf_SetTime(FUNCINF_OPERATOR_LC, FUNCINF_TIME_TYPE_SEC, *pValue);
            #endif
            break;
        }
            
        case 20: /* LC的输入状态 */
        {
            printf("[%s][%d] addr = %d\n", __func__, __LINE__, addr);
            lcInState = *pValue;
            PRINT_DEBUG("LC input state: 0x%04X", lcInState);
            if(lcInState != lcInStatePrev)
            {
                syslog(LOG_INFO, "%s: LC input state: 0x%04X -> 0x%04X", __func__, lcInStatePrev, lcInState);
                lcInStatePrev = lcInState;
            }
            #if 1
            GlobalVariable_SetInt32(GV_INT32_LC_IN_STATE, *pValue);
            GlobalVariable_SetInt32(GV_INT32_LC_IN_FAULT, *pValue & 0x2EC);
            #endif
            break;
        }
            
        case 21: /* LC的油温 */
        {
            printf("[%s][%d] addr = %d\n", __func__, __LINE__, addr);
            #if 1
            GlobalVariable_SetInt32(GV_INT32_LC_OIL_TEMP, *pValue);
            #endif
            break;
        }

        default:
        {
            printf("[%s][%d] addr = %d\n", __func__, __LINE__, addr);
            #if 0
            #endif
            break;
        }
    }
    return retVal;
}

